<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\logic\DataProcessing;
use App\logic\DrugsDataProcessing;
use App\Models\DoctorModel;
use App\servers\BaiDuTextFilter;
use Illuminate\Http\Request;

class DrugsController extends BaseController
{
    /**
     * ww
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 药品添加
     * ww
     */
    public function addDrugs(Request $request){

        try {
            $param=$request->post();
            $request->validate([
                'name'=>'required',
                'common_name'=>'required',
                'specifications'=>'required',
                'dosage_form'=>'required',
                'manufacturer'=>'required',
                'price'=>'required',
                'stock'=>'required',
            ],[
                'name.required'=>'药品名称不得为空',
                'common_name.required'=>'通用名称不得为空',
                'specifications.required'=>'规格不得为空',
                'dosage_form.required'=>'剂型不得为空',
                'manufacturer.required'=>'生产厂家不得为空',
                'price.required'=>'价格不得为空',
                'stock.required'=>'库存不得为空',
            ]);
            //点用百度云内容审核
            (new BaiDuTextFilter())->textFilter($param['name']);
            DrugsDataProcessing::adds($param);
            return $this->witejson(0,'','添加成功');
        }catch (\Exception $e){
            return $this->witejson(1,'',$e->validator->errors()->first());
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 药品删除
     * ww
     */
    public function delDrugs(Request $request)
    {
        try {
            $id=$request->get('id');
            DrugsDataProcessing::dels($id);
            return $this->witejson(0,'','删除成功');
        }catch (\Exception $e){
            return $this->witejson(1,'',$e->validator->errors()->first());
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 药品列表
     * ww
     */

    public function listDrugs(Request $request)
    {
        try {
            $data=DoctorModel::listDoctor();
            return $this->witejson(0,$data,'ok');
        }catch (\Exception $e){
            return $this->witejson(1,'',$e->validator->errors()->first());
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 药品修改
     * ww
     */
    public function updateDrugs(Request $request)
    {
        try {
            $param=$request->post();
            $id=$param['id'];
            $res=DrugsDataProcessing::updates($id,$param);
            return $this->witejson(0,'','修改成功');
        }catch (\Exception $e){
            return $this->witejson(1,'',$e->validator->errors()->first());
        }
    }
}
